package com.keon.jianbao.adapter;

import android.content.Context;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.keon.jianbao.AppContext;
import com.keon.jianbao.AppManager;
import com.keon.jianbao.R;
import com.keon.jianbao.api.Api;
import com.keon.jianbao.api.SimpleHandler;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.base.BaseListAdapter;
import com.keon.jianbao.bean.NetWorkImg;
import com.keon.jianbao.bean.Result;
import com.keon.jianbao.bean.Tweet;
import com.keon.jianbao.ui.activity.FriendInfoActivity;
import com.keon.jianbao.util.StringUtils;
import com.keon.jianbao.util.UILUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.apache.http.Header;
import org.json.JSONException;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by keon on 2015/9/2.
 */
public class TweetListAdapter extends BaseListAdapter {
    BaseActivity ba;

    public TweetListAdapter(Context context) {
        ba = (BaseActivity) context;
        addUpvoteHandler.setContext((BaseActivity) context);
    }

    TextView mUp;

    SimpleHandler addUpvoteHandler = new SimpleHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            super.onSuccess(statusCode, headers, responseBody);
            Result result = null;
            try {
                result = new Result(responseBody);
                if (result.getStatus() > 0) {
                    ba.showToast(result.getContent());
                    mUp.setText((Integer.parseInt(mUp.getText().toString()) + 1) + "");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };

    static class ViewHolder {

        @Bind(R.id.friends_tweet_avatar)
        ImageView ivAvatar;
        @Bind(R.id.friends_tweet_name)
        TextView tvName;
        @Bind(R.id.friends_tweet_time)
        TextView tvTime;
        @Bind(R.id.friends_tweet_content)
        TextView tvContent;
        @Bind(R.id.friends_tweet_up)
        TextView tvUp;
        @Bind(R.id.friends_tweet_comment)
        TextView tvComment;
        @Bind(R.id.friends_tweet_imgs)
        GridView gvImgs;
        @Bind(R.id.friends_tweet_price)
        TextView tvPrice;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    @Override
    protected View getRealView(final int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = getLayoutInflater(parent.getContext()).inflate(
                    R.layout.list_cell_tweet, null);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        final Tweet tweet = (Tweet) mDatas.get(position);

        if (tweet.getUser_name().equals("小秘书")) {
            vh.tvPrice.setVisibility(View.GONE);
        } else {
            vh.tvPrice.setText(tweet.getPrice() + "");
        }

//        vh.ivAvatar.setImageURI(Uri.parse(tweet.getUser_face()));
        ImageLoader.getInstance().displayImage(tweet.getUser_face(), vh.ivAvatar, UILUtils.getOptions1());
        vh.tvName.setText(tweet.getUser_name());
        vh.tvTime.setText(tweet.getCreate_time());
        vh.tvUp.setText(tweet.getGood() + "");
        vh.tvComment.setText(tweet.getComment() + "");
        SpannableString ss = new SpannableString("[" + tweet.getAppraisal_category() + "]  " + tweet.getContent());
        ss.setSpan(new ForegroundColorSpan(AppContext.getResource().getColor(R.color.assistTextColor)), 0, tweet.getAppraisal_category().length() + 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        vh.tvContent.setText(ss);

        initGridView(vh, tweet.getImages());

        if (!StringUtils.isEmpty(AppContext.getInstance().getProperty("user.id")) && !(Integer.parseInt(AppContext.getInstance().getProperty("user.id")) == tweet.getUser_id())) {
            vh.ivAvatar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("user_id", tweet.getUser_id());
                    BaseActivity ba = (BaseActivity) AppManager.getAppManager().currentActivity();
                    ba.jump(FriendInfoActivity.class, bundle);
                }
            });
        }


        final ViewHolder finalVh = vh;
        vh.tvUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mUp = finalVh.tvUp;
                Api.add_upvote(tweet.getId(), addUpvoteHandler);
            }
        });

        return convertView;
    }

    private void initGridView(ViewHolder vh, ArrayList<String> imgs) {
        ArrayList<NetWorkImg> data = new ArrayList<>(imgs.size());

        for (int i = 0; i < imgs.size(); i++) {
            NetWorkImg netWorkImg = new NetWorkImg(NetWorkImg.TYPE_COMMON, imgs.get(i), NetWorkImg.ACTION_ZOOM_IN);
            data.add(netWorkImg);
        }
        UploadImgAdapter adapter = new UploadImgAdapter(data);

        vh.gvImgs.setAdapter(adapter);
    }
}
